<template>
  <div>
    <a-tabs :activeKey="first_tab" @change="activeKeyChange">
      <a-tab-pane key="1" tab="應收應付">
        <a-card :bordered="false">
          <s-table
            ref="table"
            size="default"
            :columns="columns"
            :data="loadData"
            :alert="true"
            :rowKey="(record) => record.id">
            <template class="table-operator" slot="operator"  >
              <a-row>
                <a-col :span="12">
                  <a-radio-group :default-value="0" button-style="solid" @change="onSeChange">
                    <a-radio-button :value="0">
                      全部
                    </a-radio-button>
                    <a-radio-button :value="3">
                      應收
                    </a-radio-button>
                    <a-radio-button :value="4">
                      應付
                    </a-radio-button>
                  </a-radio-group>
                </a-col>
                <a-col :span="12">
                  <span class="abvv jiang">應付合計：{{ tjData.zchj }}</span>
                  <span class="ab jiang">應收合計：{{ tjData.srhj }}</span>
                </a-col>
              </a-row>
            </template>
            <span slot="typescopedSlots" slot-scope="text">
              {{ typeDicDataFilter(text) }}
            </span>
            <span slot="amount" slot-scope="text,record">
               <span  :class="record.billInOutType === 2 ? 'abvv': 'ab'" class="aa">{{ text }}</span>
            </span>
            <span slot="revenuesStatus" slot-scope="text">
            {{ text == 0 ? '未到账' : '已到账'}}
            </span>
            <span slot="action" slot-scope="text, record">
              <a  @click="$refs.lookDetail.edit(record.id)">詳情</a>
              <a-divider type="vertical" />
              <a-dropdown >
                <a class="ant-dropdown-link">
                  更多 <a-icon type="down" />
                </a>
                <a-menu slot="overlay">
                  <a-menu-item v-if="record.revenuesStatus === 0">
                    <a @click="$refs.arriveOtherForm.edit(record)">收款</a>
                  </a-menu-item>
                  <a-menu-item v-if="record.revenuesStatus === 0">
                    <a @click="$refs.editForm.edit(record)">修改</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a-popconfirm  placement="topRight" title="确认删除？" @confirm="() => billOtherRevenuesDelete(record.id)">
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                  <a-menu-item>
                    <a @click="$refs.printRevenues.edit(record.id)">打印</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </s-table>
        </a-card>
      </a-tab-pane>
      <a-tab-pane key="2" tab="已收已付">
        <a-card :bordered="false" :bodyStyle="tstyle">
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                <a-col :md="6" :sm="24">
                  <a-form-item label="">
                    <a-range-picker @change="onDateChange" />
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="收支">
                      <a-select style="width: 100%" v-model="queryParam2.billInOutType" placeholder="请选择收支方式">
                        <a-select-option  :value="0">全部</a-select-option>
                        <a-select-option  :value="1">流入</a-select-option>
                        <a-select-option  :value="2">流出</a-select-option>
                      </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="交易方式">
                      <a-select style="width: 100%" v-model="queryParam2.collectionType" placeholder="请选择交易方式" >
                        <a-select-option value="0">全部交易</a-select-option>
                        <a-select-option v-for="(item,index) in collectionTypeData" :key="index" :value="item.id">{{ item.name }}</a-select-option>
                      </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24" >
                  <span class="table-page-search-submitButtons">
                    <a-button type="primary" @click="$refs.table2.refresh(true)" >查询</a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
        </a-card>
         <a-card :bordered="false">
          <s-table
            ref="table2"
            size="default"
            :columns="columns"
            :data="loadData2"
            :alert="true"
            :rowKey="(record) => record.id">
            <template class="table-operator" slot="operator"  >
              <a-row>
                <a-col :span="12">
                </a-col>
                <a-col :span="12">
                  <span class="abvv jiang">應付合計：{{ tjData.zchj }}</span>
                  <span class="ab jiang">應收合計：{{ tjData.srhj }}</span>
                </a-col>
              </a-row>
            </template>
            <span slot="typescopedSlots" slot-scope="text">
              {{ typeDicDataFilter(text) }}
            </span>
            <span slot="amount" slot-scope="text,record">
               <span  :class="record.billInOutType === 2 ? 'abvv': 'ab'" class="aa">{{ text }}</span>
            </span>
            <span slot="revenuesStatus" slot-scope="text">
            {{ text == 0 ? '未到账' : '已到账'}}
            </span>
            <span slot="action" slot-scope="text, record">
              <a  @click="$refs.lookDetail.edit(record.id)">詳情</a>
              <a-divider type="vertical" />
              <a-dropdown >
                <a class="ant-dropdown-link">
                  更多 <a-icon type="down" />
                </a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a-popconfirm  placement="topRight" title="确认删除？" @confirm="() => billOtherRevenuesDelete(record.id)">
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                  <a-menu-item>
                    <a @click="$refs.printRevenues.edit(record.id)">打印</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </s-table>
        </a-card>
      </a-tab-pane>
      <a-button slot="tabBarExtraContent" type="primary" icon="plus" @click="$refs.addForm.add()">
        記一筆
      </a-button>
    </a-tabs>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
      <look-detail ref="lookDetail" />
      <print-revenues ref="printRevenues"/>
      <arrive-other-form ref="arriveOtherForm" @ok="handleOk"/>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { busMeansOfTransactionList } from '@/api/modular/main/busmeansoftransaction/busMeansOfTransactionManage'
  import { billOtherRevenuesPage, billOtherRevenuesDelete, getHjBySearchType } from '@/api/modular/main/billotherrevenues/billOtherRevenuesManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  import lookDetail from './lookDetail.vue'
  import printRevenues from './printRevenues.vue'
  import arriveOtherForm from './arriveOtherForm.vue'
  export default {
    components: {
      STable,
      addForm,
      editForm,
      lookDetail,
      printRevenues,
      arriveOtherForm
    },
    data () {
      return {
        first_tab: '1',
        // 查询参数
        queryParam: { searchType: 1 },
        queryParam2: { searchType: 2 },
        // 表头
        columns: [
          {
            title: '房間',
            align: 'center',
            dataIndex: 'roomName'
          },
          {
            title: '租客',
            align: 'center',
            dataIndex: 'name'
          },
          {
            title: '類型',
            align: 'center',
            dataIndex: 'type',
            scopedSlots: { customRender: 'typescopedSlots' }
          },
           {
            title: '類目',
            align: 'center',
            dataIndex: 'revenuesTypeName'
          },
          {
            title: '金额',
            align: 'center',
            dataIndex: 'amount',
            scopedSlots: { customRender: 'amount' }
          },
          {
            title: '應收日期',
            align: 'center',
            dataIndex: 'shouldReceiveDate'
          },
          {
            title: '状态',
            align: 'center',
            dataIndex: 'revenuesStatus',
            scopedSlots: { customRender: 'revenuesStatus' }
          },
          {
            title: '操作',
            width: '150px',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return billOtherRevenuesPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        loadData2: parameter => {
          return billOtherRevenuesPage(Object.assign(parameter, this.queryParam2)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        collectionTypeData: [],
        selectedRows: [],
        typeDicData: [ { id: 1, name: '收入' }, { id: 2, name: '支出' }, { id: 3, name: '應收帳' }, { id: 4, name: '應付帳' } ],
        tjData: {}
      }
    },
    created () {
      this.getTj()
      this.initData()
    },
    methods: {
       // 第一步tab点击
      activeKeyChange(key) {
        this.first_tab = key
        this.getTj()
      },
      initData() {
         busMeansOfTransactionList().then((res) => {
          if (res.code === 200) {
            this.collectionTypeData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      typeDicDataFilter (id) {
        // eslint-disable-next-line eqeqeq
        const values = this.typeDicData.filter(item => item.id == id)
        if (values.length > 0) {
          return values[0].name
        }
      },
      billOtherRevenuesDelete (id) {
        billOtherRevenuesDelete(id).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.getTj()
            this.$refs.table.refresh()
            this.$refs.table2.refresh()
          } else {
            this.$message.error(res.msg) // + res.message
          }
        })
      },
      onDateChange(date, dateString) {
        const s1 = dateString[0]
        const s2 = dateString[1]
        this.queryParam2.dateStrStart = s1
        this.queryParam2.dateStrEnd = s2
      },
      getTj() {
        getHjBySearchType(this.first_tab).then((res) => {
          if (res.code === 200) {
            this.tjData = res.data
          } else {
            this.$message.error(res.msg) // + res.message
          }
        })
      },
      onSeChange(e) {
        this.queryParam.type = e.target.value
        this.$refs.table.refresh()
      },
      handleOk () {
        this.getTj()
        this.$refs.table.refresh()
        this.$refs.table2.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less" scoped>
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
  .ab{
    color: red;
  }
  .abvv{
    color: green;
  }
  .jiang{
    float: right;
    margin-left: 10px;
    font-size: 18px;
  }
</style>
